package com.seckill.search.mq;

import com.seckill.search.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.seckill.common.constants.MqConstants.Exchange.ITEM_EXCHANGE;
import static com.seckill.common.constants.MqConstants.Key.*;


@Slf4j
@Component
public class ItemEventListener {

    @Autowired
    private IItemService itemService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.up.queue", durable = "true"),
            exchange = @Exchange(name = ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ITEM_UP_KEY
    ))
    public void listenItemUp(Long itemId){
        log.debug("监听到商品{}上架", itemId);
        itemService.handleItemUp(itemId);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.down.queue", durable = "true"),
            exchange = @Exchange(name = ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ITEM_DOWN_KEY
    ))
    public void listenItemDown(Long itemId){
        log.debug("监听到商品{}下架", itemId);
        itemService.handleItemDelete(itemId);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "search.item.expire.queue", durable = "true"),
            exchange = @Exchange(name = ITEM_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = ITEM_EXPIRE_KEY
    ))
    public void listenItemExpire(Long ItemId){
        itemService.handleItemDelete(ItemId);
    }
}
